
-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "test_audio"
VERSION = "2.0.0"

-- sys库是标配
_G.sys = require("sys")
_G.sysplus = require("sysplus")

log.style(1)
--require "rotary"


require "music_demo"
require "st7789"
init_st7789()

--旋转编码器调用请取消下面代码注解
--rotary_start()


--温湿度传感器 AHT10 显示，不用请删除
--[[
local aht10 = require "aht10"
i2cid = 0
i2c_speed = i2c.FAST

sys.taskInit(function()
    i2c.setup(i2cid,i2c_speed)
    aht10.init(i2cid)--初始化,传入i2c_id
   -- local aht10_data = aht10.get_data()
   -- local count = 0

   --把传感器数据只保留小数点后两位并四舍五入
function keepDecimalTest(num, n)

    if type(num) ~= "number" then
        return num    
    end
  --  n = n or 2
    return string.format("%." .. n .. "f", num)
end
lcd.setFont(lcd.font_opposansm12_chinese)


    while 1 do
        --sys.wait(100)
        local aht10_data = aht10.get_data()
       -- log.info("aht10_data", "aht10_data.RH:"..(aht10_data.RH*100).."%","aht10_data.T"..(aht10_data.T).."℃")
        --lcd.fill(0,0,160,40)
        num1=(aht10_data.RH*100)  --基于小米蓝牙温湿度计比对调的湿度原始数据+/-差异值或者直接修改传感器读取代码，确保串口和屏幕信息一致
        num2=(aht10_data.T)    --基于小米蓝牙温湿度计比对调的温度原始数据+/-差异值或者直接修改传感器读取代码，确保串口和屏幕信息一致
        humidity=keepDecimalTest(num1, 2)
        temp=keepDecimalTest(num2, 2)
     --   timer.mdelay(100)
        lcd.fill(200,0,320,70,0xffff)
        --sys.wait(50)
        lcd.drawStr(210,20,"湿度: "..(humidity).."%",0x0000)
        
        lcd.drawStr(210,40,"温度: "..(temp).."度",0x0000)
        --[[adc.open(1) -- 模块上的ADC1脚-PA4, 0~2.4v,不要超过范围使用!!!
        a = {adc.read(0)}
       -- print(adc.read(0))
       -- num3 = (a[2] * 4)/1000
        num3 = (a[2])/1000
        v = keepDecimalTest(num3 , 1)
        v1 = v * 1000
        if v1 >= 3800 then
          --  num3 = (v - 3700)/(4200 - 3700) * 100
           -- vbat = keepDecimalTest(num3,0)
          -- pm.request(pm.IDLE)
           lcd.on()
            lcd.drawStr(210,60,"电池: "..(v).."v",0x0000)
           -- lcd.drawStr(210,60,"电池: "..(vbat).."mv",0x0000)
        elseif v1 < 3800 and v1 >= 3700 then
            lcd.drawStr(210,60,"电池: 电压低",0x0000)

         --   lcd.close()
           -- pm.request(pm.DEEP)
        elseif v1 < 3700 then
            lcd.drawStr(210,60,"电池: 无电池",0x0000)
          --  lcd.close()
           -- pm.request(pm.DEEP)
        end
        adc.close(0)]]
        sys.wait(80)

--end
--end)


local LEDA = gpio.setup(pin.PC3, 0, gpio.PULLUP) -- PE07输出模式,内部上拉
 local LEDB = gpio.setup(pin.PD14, 0, gpio.PULLUP) -- PE06输出模式,内部上拉
 local LEDC = gpio.setup(pin.PD15, 0, gpio.PULLUP) -- PB10输出模式,内部上拉

sys.taskInit(function()
    while 1 do
        sys.wait(200)
        -- 一闪一闪亮晶晶
     LEDA(1)
     sys.wait(200)
     LEDB(1)
     sys.wait(200)
     LEDC(1)
sys.wait(200)
LEDA(0)
sys.wait(200)
     LEDB(0)
     sys.wait(200)
     LEDC(0)
     --[[
        if count >=3 and count <6 then
        LEDA(count % 3 == 0 and 1 or 0)
        --print("a"..count)
        LEDB(count % 3 == 1 and 1 or 0)
      --  print("b"..count)
        LEDC(count % 3 == 2 and 1 or 0)
      --  print("c"..count)
        end
        if count >= 6 and count < 9 then
            LEDC(count % 3 == 0 and 1 or 0)
            --print("a"..count)
            LEDB(count % 3 == 1 and 1 or 0)
          --  print("b"..count)
            LEDA(count % 3 == 2 and 1 or 0)
            
        
        end
        if count == 9 then
            count =3
        end
        --log.info("gpio", "Go Go Go", count, rtos.bsp())
        count = count + 1
        ]]
    end
end)

--播放MP3 ，请根据需要自己调整
sys.taskInit(function()

   -- lcd.clear()
  
lcd.fill(0,0,320,240,0x0000)
sys.wait(100)
lcd.setColor(0x77FF,0x0000)
sys.wait(100)
lcd.setFont(lcd.font_opposansm12_chinese)
lcd.drawStr(50,25,"MP3 Demo",0XE1FF)
lcd.drawStr(0,45,"暂停请按红灯旁边按钮",0XFF9999)
lcd.drawStr(0,65,"路径:/sd".. musicDir,0XFF9999)
-- lcd.drawStr(50,25,"MP3 Demo",0XE1FF)

end)
-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
